#include "GameSounds.h"
#include <QCoreApplication>
#include "toolkit.h"

#ifdef Q_OS_SYMBIAN
#include "sound/playerutility.h"
#endif

GameSounds::GameSounds(QObject *parent) :
    QObject(parent)
{
    init();
}

GameSounds::~GameSounds()
{
#ifdef Q_OS_SYMBIAN
    //    delete m_bgSound;
    for(int i = 0;i < MAX_AUDIOEFFECT;i++)
        delete m_sounds[i];
#endif
}

void GameSounds::init()
{
#ifdef Q_OS_SYMBIAN
    QString path = QCoreApplication::applicationDirPath();
    path.replace("/","\\");

    m_sounds[0] = new PlayerUtility(path + "\\Resources\\GO.wav");
    m_sounds[1] = new PlayerUtility(path + "\\Resources\\READY.wav");
    m_sounds[2] = new PlayerUtility(path + "\\Resources\\click.wav");
    m_sounds[3] = new PlayerUtility(path + "\\Resources\\catch.wav");
    m_sounds[4] = new PlayerUtility(path + "\\Resources\\bigclick.wav");

    m_sounds[5] = new PlayerUtility(path + "\\Resources\\crash.wav");
    m_sounds[6] = new PlayerUtility(path + "\\Resources\\crash.wav");
    m_sounds[7] = new PlayerUtility(path + "\\Resources\\crash.wav");

    m_sounds[8] = new PlayerUtility(path + "\\Resources\\path.wav");
    m_sounds[9] = new PlayerUtility(path + "\\Resources\\path.wav");
    m_sounds[10] = new PlayerUtility(path + "\\Resources\\path.wav");

    m_sounds[11] = new PlayerUtility(path + "\\Resources\\opendoor.wav");
    m_sounds[12] = new PlayerUtility(path + "\\Resources\\opendoor.wav");
    m_sounds[13] = new PlayerUtility(path + "\\Resources\\opendoor.wav");

    m_sounds[14] = new PlayerUtility(path + "\\Resources\\offdoor.wav");
    m_sounds[15] = new PlayerUtility(path + "\\Resources\\offdoor.wav");
    m_sounds[16] = new PlayerUtility(path + "\\Resources\\offdoor.wav");

    m_sounds[17] = new PlayerUtility(path + "\\Resources\\flybomb.wav");
    m_sounds[18] = new PlayerUtility(path + "\\Resources\\flybomb.wav");
    m_sounds[19] = new PlayerUtility(path + "\\Resources\\flybomb.wav");

    m_sounds[20] = new PlayerUtility(path + "\\Resources\\flybreak.wav");
    m_sounds[21] = new PlayerUtility(path + "\\Resources\\flybreak.wav");
    m_sounds[22] = new PlayerUtility(path + "\\Resources\\flybreak.wav");

    m_sounds[23] = new PlayerUtility(path + "\\Resources\\flyhit.wav");
    m_sounds[24] = new PlayerUtility(path + "\\Resources\\flyhit.wav");
    m_sounds[25] = new PlayerUtility(path + "\\Resources\\flyhit.wav");

    m_sounds[26] = new PlayerUtility(path + "\\Resources\\run.wav");
    m_sounds[27] = new PlayerUtility(path + "\\Resources\\run.wav");
    m_sounds[28] = new PlayerUtility(path + "\\Resources\\run.wav");

    m_sounds[29] = new PlayerUtility(path + "\\Resources\\combo.wav");
    m_sounds[30] = new PlayerUtility(path + "\\Resources\\clear.wav");
    m_sounds[31] = new PlayerUtility(path + "\\Resources\\comboo.wav");
    m_sounds[32] = new PlayerUtility(path + "\\Resources\\faid.wav");
    m_sounds[33] = new PlayerUtility(path + "\\Resources\\getin.wav");
    m_sounds[34] = new PlayerUtility(path + "\\Resources\\link.wav");

    m_sounds[35] = new PlayerUtility(path + "\\Resources\\pao.wav");
    m_sounds[36] = new PlayerUtility(path + "\\Resources\\pao.wav");
    m_sounds[37] = new PlayerUtility(path + "\\Resources\\pao.wav");

    m_bgSound[0] = new PlayerUtility((path + "\\Resources\\menu.mp3"),true);
    m_bgSound[0]->setLoops(true);
    m_bgSound[0]->setImmediately(ToolKit::m_bVoice);

    m_bgSound[1] = new PlayerUtility((path + "\\Resources\\game.mp3"),true);
    m_bgSound[1]->setLoops(true);
#endif
}

void GameSounds::playBgSound(int index)
{
#ifdef Q_OS_SYMBIAN
    if(index >= 0 && index < MAX_BGSOUND)
        m_bgSound[index]->play();
#endif
}

void GameSounds::stop(int index)
{
#ifdef Q_OS_SYMBIAN
    if(index >= 0 && index < MAX_BGSOUND)
        m_bgSound[index]->stop();
#endif
}

void GameSounds::play(int index)
{
    static int icrash = 5;
    static int ipath = 8;
    static int iopendoor = 11;
    static int ioffdoor = 14;
    static int iflybomb = 17;
    static int iflybreak = 20;
    static int iflyhit = 23;
    static int irun = 26;
    static int icannon = 35;

    switch(index)
    {
    case 5:
    {
        index = icrash++;
        if(icrash > 7)
        {
            icrash = 5;
        }
    }
    break;
    case 6:
    {
        index = ipath++;
        if(ipath > 10)
        {
            ipath = 8;
        }
    }
    break;
    case 7:
    {
        index = iopendoor++;
        if(iopendoor > 13)
        {
            iopendoor = 11;
        }
    }
    break;
    case 8:
    {
        index = ioffdoor++;
        if(ioffdoor > 16)
        {
            ioffdoor = 14;
        }
    }
    break;
    case 9:
    {
        index = iflybomb++;
        if(iflybomb > 19)
        {
            iflybomb = 17;
        }
    }
    break;
    case 10:
    {
        index = iflybreak++;
        if(iflybreak > 22)
        {
            iflybreak = 20;
        }
    }
    break;
    case 11:
    {
        index = iflyhit++;
        if(iflyhit > 25)
        {
            iflyhit = 23;
        }
    }
    break;
    case 12:
    {
        index = irun++;
        if(irun > 28)
        {
            irun = 26;
        }
    }
    break;
    case 13:
    {
        index = icannon++;
        if(icannon > 37)
        {
            icannon = 35;
        }
    }
    default:
        break;
    }

#ifdef Q_OS_SYMBIAN
    if(index >= 0 && index < MAX_AUDIOEFFECT)
        m_sounds[index]->play();
#endif
}

bool GameSounds::IsPlaying(int index)
{
#ifdef Q_OS_SYMBIAN
    if(index >= 0 && index < MAX_BGSOUND)
       return m_bgSound[index]->IsPlaying();
#endif
}
